namespace NFTRedit
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Windows.Forms;

    public class MapForm : Form
    {
        private static ImageAttributes attr;
        private Button buttonApply;
        private static ColorMap[] colorMap = new ColorMap[] { new ColorMap() };
        private IContainer components;
        private NFTRedit.Font font;
        private Bitmap gridBuffer;
        private GroupBox groupBoxTile;
        private int hoverIndex = -1;
        private static ColorMap[] hoverMap;
        private int hoverX = -1;
        private int hoverY = -1;
        private bool isLoading;
        private Label labelBy;
        private Label labelCharacter;
        private Label labelMapping;
        private Label labelNextTile;
        private Label labelOffset;
        private Label labelWidth;
        private Bitmap mapBuffer;
        private int numCols = 1;
        private int numRows = 1;
        private static Color panelBackgroundColor = Color.FromArgb(0xef, 0xde, 0xc6);
        private ScrollPanel panelMap;
        private Panel panelPen;
        private Panel panelTile;
        private MainForm parent;
        private PictureBox pictureBoxMap;
        private static readonly int scale = 8;
        private int selectedIndex = -1;
        private int selectedIntensity;
        private static ColorMap[] selectedMap;
        private CharacterTable table;
        private TextBox textBoxCharacter;
        private TextBox textBoxMapping;
        private TextBox textBoxMaxHeight;
        private TextBox textBoxMaxWidth;
        private TextBox textBoxNextOffset;
        private TextBox textBoxOffset;
        private TextBox textBoxWidth;
        private static Color tileBackgroundColor = Color.FromArgb(0, 0, 0, 0);
        private Bitmap tileBuffer;
        private TrackBar trackBarIntensity;
        private List<char> validDec = new List<char>(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });

        static MapForm()
        {
            colorMap[0].OldColor = Color.FromArgb(0, 0, 0, 0);
            colorMap[0].NewColor = Color.FromArgb(0xef, 0xde, 0xc6);
            hoverMap = new ColorMap[] { new ColorMap() };
            hoverMap[0].OldColor = Color.FromArgb(0, 0, 0, 0);
            hoverMap[0].NewColor = Color.FromArgb(0xff, 0xff, 0x66);
            selectedMap = new ColorMap[] { new ColorMap() };
            selectedMap[0].OldColor = Color.FromArgb(0, 0, 0, 0);
            selectedMap[0].NewColor = Color.FromArgb(0x99, 0xff, 0x33);
            attr = new ImageAttributes();
            attr.SetRemapTable(colorMap);
        }

        public MapForm()
        {
            this.InitializeComponent();
        }

        private void ApplyCharacter()
        {
            Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            if (this.textBoxCharacter.Text.Length == 0)
            {
                if (this.table.Exists(tileByIndex.Code))
                {
                    this.table.Remove(tileByIndex.Code);
                }
            }
            else
            {
                char c = this.textBoxCharacter.Text[0];
                if (this.table.Exists(c))
                {
                    if (this.table.GetCodeByChar(c) != tileByIndex.Code)
                    {
                        MessageBox.Show("Error: Character is already in use", "Invalid value");
                        this.textBoxCharacter.Focus();
                    }
                }
                else
                {
                    this.table.Reassign(tileByIndex.Code, c);
                }
            }
        }

        private void ApplyNextOffset()
        {
            Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            byte num = byte.Parse(this.textBoxNextOffset.Text);
            this.font.Properties[this.selectedIndex][2] = num;
            tileByIndex.Width = num;
        }

        private void ApplyOffset()
        {
            Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            byte num = byte.Parse(this.textBoxOffset.Text);
            this.font.Properties[this.selectedIndex][0] = num;
            tileByIndex.OffsetX = num;
        }

        private void ApplyTile()
        {
            Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            tileByIndex.Tile.Dispose();
            tileByIndex.Tile = new Bitmap(this.tileBuffer);
            this.font.Tiles[this.selectedIndex] = tileByIndex.Tile;
            this.DrawTile(this.selectedIndex);
        }

        private void ApplyWidth()
        {
            Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            byte num = byte.Parse(this.textBoxWidth.Text);
            this.font.Properties[this.selectedIndex][1] = num;
            tileByIndex.LetterWidth = num;
        }

        private void ButtonApplyClick(object sender, EventArgs e)
        {
            this.ApplyCharacter();
            this.ApplyOffset();
            this.ApplyWidth();
            this.ApplyNextOffset();
            this.ApplyTile();
            this.buttonApply.Enabled = false;
            this.parent.RedrawDialogBox();
        }

        private void ChangeHover(int x, int y, int index)
        {
            int hoverIndex = this.hoverIndex;
            this.hoverX = x;
            this.hoverY = y;
            this.hoverIndex = index;
            if (hoverIndex != -1)
            {
                this.DrawTile(hoverIndex);
            }
            if (this.hoverIndex != -1)
            {
                this.DrawTile(this.hoverIndex);
            }
            this.pictureBoxMap.Invalidate();
            if (this.selectedIndex == -1)
            {
                this.LoadTileFields();
            }
        }

        private void ChangeSelected(int index)
        {
            int selectedIndex = this.selectedIndex;
            this.selectedIndex = index;
            if (selectedIndex != -1)
            {
                this.DrawTile(selectedIndex);
            }
            if (this.selectedIndex != -1)
            {
                this.DrawTile(this.selectedIndex);
            }
            this.pictureBoxMap.Invalidate();
            if (this.selectedIndex != -1)
            {
                int num1 = this.selectedIndex % this.numCols;
                int num2 = this.selectedIndex / this.numCols;
                int y = num2 * (this.font.MaxHeight + 1);
                int num4 = -this.panelMap.AutoScrollPosition.Y;
                if (num4 > y)
                {
                    this.panelMap.AutoScrollPosition = new Point(0, y);
                }
                int num5 = ((num2 + 1) * (this.font.MaxHeight + 1)) + 3;
                int num6 = this.panelMap.Height - this.panelMap.AutoScrollPosition.Y;
                if (num6 < num5)
                {
                    this.panelMap.AutoScrollPosition = new Point(0, num5 - this.panelMap.Height);
                }
            }
            this.LoadTileFields();
        }

        private void CopyCharacter()
        {
            if ((this.selectedIndex >= 0) && (this.selectedIndex < this.font.Tiles.Length))
            {
                Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
                Clipboard.SetData("NFTRChar", this.Serialize(tileByIndex));
            }
        }

        private object Deserialize(string serializationString)
        {
            using (MemoryStream stream = new MemoryStream(Convert.FromBase64String(serializationString)))
            {
                BinaryFormatter formatter = new BinaryFormatter();
                return formatter.Deserialize(stream);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void DrawMap()
        {
            this.numRows = (int) Math.Ceiling((double) ((1.0 * this.font.Tiles.Length) / ((double) this.numCols)));
            int num = this.font.MaxWidth + 1;
            int num2 = this.font.MaxHeight + 1;
            this.mapBuffer = new Bitmap((this.numCols * num) - 1, (this.numRows * num2) - 1);
            Graphics graphics = Graphics.FromImage(this.mapBuffer);
            graphics.Clear(Color.White);
            graphics.Dispose();
            for (int i = 0; i < this.font.Tiles.Length; i++)
            {
                this.DrawTile(i);
            }
            this.pictureBoxMap.Image = this.mapBuffer;
        }

        private void DrawPixel(int x, int y, int intensity)
        {
            if ((this.selectedIndex != -1) && (((x >= 0) && (x < this.font.MaxWidth)) && ((y >= 0) && (y < this.font.MaxHeight))))
            {
                Graphics graphics = Graphics.FromImage(this.tileBuffer);
                this.tileBuffer.SetPixel(x, y, tileBackgroundColor);
                graphics.DrawImageUnscaled(this.font.Dots[intensity], x, y);
                graphics.Dispose();
                this.panelTile.Invalidate();
                if (!Utility.AutoApply)
                {
                    this.buttonApply.Enabled = true;
                }
                else
                {
                    this.ApplyTile();
                    this.parent.RedrawDialogBox();
                }
            }
        }

        private void DrawTile(int index)
        {
            if ((index >= 0) && (index < this.font.Tiles.Length))
            {
                int num = this.font.MaxWidth + 1;
                int num2 = this.font.MaxHeight + 1;
                int x = (index % this.numCols) * num;
                int y = (index / this.numCols) * num2;
                Rectangle rect = new Rectangle(x, y, this.font.MaxWidth, this.font.MaxHeight);
                Graphics graphics = Graphics.FromImage(this.mapBuffer);
                Brush brush = new SolidBrush(Color.White);
                graphics.FillRectangle(brush, rect);
                if (index == this.selectedIndex)
                {
                    attr.SetRemapTable(selectedMap);
                }
                else if (index == this.hoverIndex)
                {
                    attr.SetRemapTable(hoverMap);
                }
                graphics.DrawImage(this.font.Tiles[index], rect, 0, 0, this.font.MaxWidth, this.font.MaxHeight, GraphicsUnit.Pixel, attr);
                attr.SetRemapTable(colorMap);
                graphics.Dispose();
                this.pictureBoxMap.Invalidate();
            }
        }

        private void Initialize()
        {
            this.trackBarIntensity.Maximum = this.font.Dots.Length - 1;
            this.trackBarIntensity.Value = this.trackBarIntensity.Maximum;
            this.selectedIntensity = this.trackBarIntensity.Maximum;
            this.textBoxMaxWidth.Text = "" + this.font.MaxWidth;
            this.textBoxMaxHeight.Text = "" + this.font.MaxHeight;
            int num = ((scale * this.font.MaxWidth) + 2) - this.panelTile.Width;
            int num2 = ((scale * this.font.MaxHeight) + 2) - this.panelTile.Height;
            if ((this.groupBoxTile.Width + num) < this.groupBoxTile.MinimumSize.Width)
            {
                num = this.groupBoxTile.MinimumSize.Width - this.groupBoxTile.Width;
            }
            if ((this.groupBoxTile.Height + num2) < this.groupBoxTile.MinimumSize.Height)
            {
                num2 = this.groupBoxTile.MinimumSize.Height - this.groupBoxTile.Height;
            }
            this.panelMap.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            base.Size = new Size(base.Width + num, base.Height + num2);
            this.groupBoxTile.Left -= num;
            this.groupBoxTile.Width += num;
            this.groupBoxTile.Height += num2;
            this.buttonApply.Top += num2;
            this.labelNextTile.Top += num2;
            this.textBoxNextOffset.Top += num2;
            this.labelOffset.Top += num2;
            this.textBoxOffset.Top += num2;
            this.labelWidth.Top += num2;
            this.textBoxWidth.Top += num2;
            this.panelTile.Width = (scale * this.font.MaxWidth) + 2;
            this.panelTile.Height = (scale * this.font.MaxHeight) + 2;
            this.tileBuffer = new Bitmap(this.font.MaxWidth, this.font.MaxHeight);
            this.gridBuffer = new Bitmap(this.panelTile.Width, this.panelTile.Height);
            Graphics graphics = Graphics.FromImage(this.gridBuffer);
            graphics.Clear(Color.Transparent);
            Pen pen = new Pen(Color.Red);
            for (int i = 1; i < this.font.MaxHeight; i++)
            {
                graphics.DrawLine(pen, 0, i * scale, this.panelTile.Width, i * scale);
            }
            for (int j = 1; j < this.font.MaxWidth; j++)
            {
                graphics.DrawLine(pen, j * scale, 0, j * scale, this.panelTile.Height);
            }
            graphics.Dispose();
            this.panelMap.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.RecalculateMapDimensions();
        }

        private void InitializeComponent()
        {
            ComponentResourceManager manager = new ComponentResourceManager(typeof(MapForm));
            this.panelMap = new ScrollPanel();
            this.pictureBoxMap = new PictureBox();
            this.textBoxMaxWidth = new TextBox();
            this.labelBy = new Label();
            this.textBoxMaxHeight = new TextBox();
            this.groupBoxTile = new GroupBox();
            this.labelCharacter = new Label();
            this.textBoxCharacter = new TextBox();
            this.panelPen = new Panel();
            this.textBoxMapping = new TextBox();
            this.labelMapping = new Label();
            this.buttonApply = new Button();
            this.labelNextTile = new Label();
            this.labelOffset = new Label();
            this.labelWidth = new Label();
            this.textBoxNextOffset = new TextBox();
            this.textBoxOffset = new TextBox();
            this.textBoxWidth = new TextBox();
            this.panelTile = new Panel();
            this.trackBarIntensity = new TrackBar();
            this.panelMap.SuspendLayout();
            ((ISupportInitialize) this.pictureBoxMap).BeginInit();
            this.groupBoxTile.SuspendLayout();
            this.trackBarIntensity.BeginInit();
            base.SuspendLayout();
            this.panelMap.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Top;
            this.panelMap.AutoScroll = true;
            this.panelMap.BorderStyle = BorderStyle.Fixed3D;
            this.panelMap.Controls.Add(this.pictureBoxMap);
            this.panelMap.Location = new Point(12, 12);
            this.panelMap.Name = "panelMap";
            this.panelMap.Size = new Size(0xcc, 0x14f);
            this.panelMap.TabIndex = 0;
            this.pictureBoxMap.Location = new Point(0, 0);
            this.pictureBoxMap.Margin = new Padding(0);
            this.pictureBoxMap.Name = "pictureBoxMap";
            this.pictureBoxMap.Size = new Size(200, 0x14b);
            this.pictureBoxMap.SizeMode = PictureBoxSizeMode.AutoSize;
            this.pictureBoxMap.TabIndex = 0;
            this.pictureBoxMap.TabStop = false;
            this.pictureBoxMap.MouseLeave += new EventHandler(this.PictureBoxMapMouseLeave);
            this.pictureBoxMap.PreviewKeyDown += new PreviewKeyDownEventHandler(this.PictureBoxMapPreviewKeyDown);
            this.pictureBoxMap.MouseMove += new MouseEventHandler(this.PictureBoxMapMouseMove);
            this.pictureBoxMap.Click += new EventHandler(this.PictureBoxMapClick);
            this.textBoxMaxWidth.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.textBoxMaxWidth.Location = new Point(250, 11);
            this.textBoxMaxWidth.Name = "textBoxMaxWidth";
            this.textBoxMaxWidth.ReadOnly = true;
            this.textBoxMaxWidth.Size = new Size(20, 20);
            this.textBoxMaxWidth.TabIndex = 1;
            this.textBoxMaxWidth.TabStop = false;
            this.labelBy.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.labelBy.Location = new Point(0x114, 14);
            this.labelBy.Name = "labelBy";
            this.labelBy.Size = new Size(13, 0x11);
            this.labelBy.TabIndex = 2;
            this.labelBy.Text = "\x00d7";
            this.textBoxMaxHeight.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.textBoxMaxHeight.Location = new Point(0x127, 11);
            this.textBoxMaxHeight.Name = "textBoxMaxHeight";
            this.textBoxMaxHeight.ReadOnly = true;
            this.textBoxMaxHeight.Size = new Size(20, 20);
            this.textBoxMaxHeight.TabIndex = 3;
            this.textBoxMaxHeight.TabStop = false;
            this.groupBoxTile.Anchor = AnchorStyles.Right | AnchorStyles.Top;
            this.groupBoxTile.Controls.Add(this.labelCharacter);
            this.groupBoxTile.Controls.Add(this.textBoxCharacter);
            this.groupBoxTile.Controls.Add(this.panelPen);
            this.groupBoxTile.Controls.Add(this.textBoxMapping);
            this.groupBoxTile.Controls.Add(this.labelMapping);
            this.groupBoxTile.Controls.Add(this.buttonApply);
            this.groupBoxTile.Controls.Add(this.labelNextTile);
            this.groupBoxTile.Controls.Add(this.labelOffset);
            this.groupBoxTile.Controls.Add(this.labelWidth);
            this.groupBoxTile.Controls.Add(this.textBoxNextOffset);
            this.groupBoxTile.Controls.Add(this.textBoxOffset);
            this.groupBoxTile.Controls.Add(this.textBoxWidth);
            this.groupBoxTile.Controls.Add(this.panelTile);
            this.groupBoxTile.Controls.Add(this.trackBarIntensity);
            this.groupBoxTile.Location = new Point(230, 0x25);
            this.groupBoxTile.MinimumSize = new Size(110, 0x11c);
            this.groupBoxTile.Name = "groupBoxTile";
            this.groupBoxTile.Size = new Size(110, 310);
            this.groupBoxTile.TabIndex = 4;
            this.groupBoxTile.TabStop = false;
            this.groupBoxTile.Text = "Tile";
            this.labelCharacter.Location = new Point(8, 0x2a);
            this.labelCharacter.Name = "labelCharacter";
            this.labelCharacter.Size = new Size(0x38, 0x11);
            this.labelCharacter.TabIndex = 2;
            this.labelCharacter.Text = "Character:";
            this.textBoxCharacter.Location = new Point(0x4d, 0x27);
            this.textBoxCharacter.MaxLength = 1;
            this.textBoxCharacter.Name = "textBoxCharacter";
            this.textBoxCharacter.Size = new Size(20, 20);
            this.textBoxCharacter.TabIndex = 3;
            this.textBoxCharacter.TextChanged += new EventHandler(this.TextBoxTextChanged);
            this.panelPen.BackColor = Color.Black;
            this.panelPen.BorderStyle = BorderStyle.Fixed3D;
            this.panelPen.Location = new Point(6, 0x49);
            this.panelPen.Name = "panelPen";
            this.panelPen.Size = new Size(20, 20);
            this.panelPen.TabIndex = 4;
            this.textBoxMapping.Location = new Point(0x41, 13);
            this.textBoxMapping.MaxLength = 2;
            this.textBoxMapping.Name = "textBoxMapping";
            this.textBoxMapping.ReadOnly = true;
            this.textBoxMapping.Size = new Size(0x20, 20);
            this.textBoxMapping.TabIndex = 1;
            this.labelMapping.Location = new Point(8, 0x10);
            this.labelMapping.Name = "labelMapping";
            this.labelMapping.Size = new Size(0x33, 0x11);
            this.labelMapping.TabIndex = 0;
            this.labelMapping.Text = "Mapping:";
            this.buttonApply.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.buttonApply.Enabled = false;
            this.buttonApply.Location = new Point(6, 0x119);
            this.buttonApply.Name = "buttonApply";
            this.buttonApply.Size = new Size(0x62, 0x17);
            this.buttonApply.TabIndex = 13;
            this.buttonApply.Text = "Apply Changes";
            this.buttonApply.UseVisualStyleBackColor = true;
            this.buttonApply.Click += new EventHandler(this.ButtonApplyClick);
            this.labelNextTile.Location = new Point(8, 0x102);
            this.labelNextTile.Name = "labelNextTile";
            this.labelNextTile.Size = new Size(0x3f, 0x11);
            this.labelNextTile.TabIndex = 11;
            this.labelNextTile.Text = "Next Offset:";
            this.labelOffset.Location = new Point(0x21, 0xe8);
            this.labelOffset.Name = "labelOffset";
            this.labelOffset.Size = new Size(0x26, 0x11);
            this.labelOffset.TabIndex = 9;
            this.labelOffset.Text = "Offset:";
            this.labelWidth.Location = new Point(0x21, 0xce);
            this.labelWidth.Name = "labelWidth";
            this.labelWidth.Size = new Size(0x26, 0x11);
            this.labelWidth.TabIndex = 7;
            this.labelWidth.Text = "Width:";
            this.textBoxNextOffset.Location = new Point(0x4d, 0xff);
            this.textBoxNextOffset.MaxLength = 2;
            this.textBoxNextOffset.Name = "textBoxNextOffset";
            this.textBoxNextOffset.Size = new Size(20, 20);
            this.textBoxNextOffset.TabIndex = 12;
            this.textBoxNextOffset.TextChanged += new EventHandler(this.TextBoxTextChanged);
            this.textBoxNextOffset.KeyPress += new KeyPressEventHandler(this.TextBoxKeyPress);
            this.textBoxOffset.Location = new Point(0x4d, 0xe5);
            this.textBoxOffset.MaxLength = 2;
            this.textBoxOffset.Name = "textBoxOffset";
            this.textBoxOffset.Size = new Size(20, 20);
            this.textBoxOffset.TabIndex = 10;
            this.textBoxOffset.TextChanged += new EventHandler(this.TextBoxTextChanged);
            this.textBoxOffset.KeyPress += new KeyPressEventHandler(this.TextBoxKeyPress);
            this.textBoxWidth.Location = new Point(0x4d, 0xcb);
            this.textBoxWidth.MaxLength = 2;
            this.textBoxWidth.Name = "textBoxWidth";
            this.textBoxWidth.Size = new Size(20, 20);
            this.textBoxWidth.TabIndex = 8;
            this.textBoxWidth.TextChanged += new EventHandler(this.TextBoxTextChanged);
            this.textBoxWidth.KeyPress += new KeyPressEventHandler(this.TextBoxKeyPress);
            this.panelTile.Anchor = AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top;
            this.panelTile.BorderStyle = BorderStyle.FixedSingle;
            this.panelTile.Location = new Point(6, 0x63);
            this.panelTile.Name = "panelTile";
            this.panelTile.Size = new Size(0x62, 0x62);
            this.panelTile.TabIndex = 6;
            this.panelTile.MouseWheel += new MouseEventHandler(this.PanelTileMouseWheel);
            this.panelTile.Paint += new PaintEventHandler(this.PanelTilePaint);
            this.panelTile.MouseMove += new MouseEventHandler(this.PanelTileMouseMove);
            this.panelTile.MouseDown += new MouseEventHandler(this.PanelTileMouseDown);
            this.trackBarIntensity.LargeChange = 1;
            this.trackBarIntensity.Location = new Point(20, 0x41);
            this.trackBarIntensity.Maximum = 1;
            this.trackBarIntensity.Name = "trackBarIntensity";
            this.trackBarIntensity.Size = new Size(0x54, 0x2d);
            this.trackBarIntensity.TabIndex = 5;
            this.trackBarIntensity.Value = 1;
            this.trackBarIntensity.Scroll += new EventHandler(this.TrackBarIntensityScroll);
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x160, 0x167);
            base.Controls.Add(this.groupBoxTile);
            base.Controls.Add(this.textBoxMaxHeight);
            base.Controls.Add(this.labelBy);
            base.Controls.Add(this.textBoxMaxWidth);
            base.Controls.Add(this.panelMap);
            this.DoubleBuffered = true;
            base.Icon = (Icon) manager.GetObject("$this.Icon");
            this.MinimumSize = new Size(200, 0x189);
            base.Name = "MapForm";
            this.Text = "Character Map";
            base.Resize += new EventHandler(this.MapFormResize);
            this.panelMap.ResumeLayout(false);
            this.panelMap.PerformLayout();
            ((ISupportInitialize) this.pictureBoxMap).EndInit();
            this.groupBoxTile.ResumeLayout(false);
            this.groupBoxTile.PerformLayout();
            this.trackBarIntensity.EndInit();
            base.ResumeLayout(false);
            base.PerformLayout();
        }

        private void LoadTileFields()
        {
            Character tileByIndex = null;
            this.isLoading = true;
            if (this.selectedIndex >= 0)
            {
                tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
            }
            else if (this.hoverIndex >= 0)
            {
                tileByIndex = this.font.GetTileByIndex((ushort) this.hoverIndex);
            }
            if (tileByIndex != null)
            {
                this.textBoxMapping.Text = string.Format("{0:x4}", tileByIndex.Code);
                if (this.table.Exists(tileByIndex.Code))
                {
                    this.textBoxCharacter.Text = "" + this.table.GetCharByCode(tileByIndex.Code);
                }
                else
                {
                    this.textBoxCharacter.Text = "";
                }
                this.tileBuffer = new Bitmap(tileByIndex.Tile);
                this.textBoxWidth.Text = "" + tileByIndex.LetterWidth;
                this.textBoxOffset.Text = "" + tileByIndex.OffsetX;
                this.textBoxNextOffset.Text = "" + tileByIndex.Width;
            }
            else
            {
                this.textBoxMapping.Text = "";
                this.textBoxCharacter.Text = "";
                this.tileBuffer = new Bitmap(this.font.MaxWidth, this.font.MaxHeight);
                this.textBoxWidth.Text = "";
                this.textBoxOffset.Text = "";
                this.textBoxNextOffset.Text = "";
            }
            this.buttonApply.Enabled = false;
            this.isLoading = false;
            this.panelTile.Invalidate();
        }

        private void MapFormResize(object sender, EventArgs e)
        {
            this.RecalculateMapDimensions();
        }

        private void PanelTileMouseDown(object sender, MouseEventArgs e)
        {
            int x = e.X / scale;
            int y = e.Y / scale;
            if (e.Button == MouseButtons.Left)
            {
                this.DrawPixel(x, y, this.selectedIntensity);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.DrawPixel(x, y, 0);
            }
            this.panelTile.Select();
        }

        private void PanelTileMouseMove(object sender, MouseEventArgs e)
        {
            int x = e.X / scale;
            int y = e.Y / scale;
            if (e.Button == MouseButtons.Left)
            {
                this.DrawPixel(x, y, this.selectedIntensity);
            }
            else if (e.Button == MouseButtons.Right)
            {
                this.DrawPixel(x, y, 0);
            }
        }

        private void PanelTileMouseWheel(object sender, MouseEventArgs e)
        {
            int num = this.selectedIntensity + (e.Delta / 120);
            if (num < 0)
            {
                num = 0;
            }
            else if (num >= this.font.Dots.Length)
            {
                num = this.font.Dots.Length - 1;
            }
            this.isLoading = true;
            this.selectedIntensity = num;
            this.trackBarIntensity.Value = this.selectedIntensity;
            this.isLoading = false;
        }

        private void PanelTilePaint(object sender, PaintEventArgs e)
        {
            Graphics graphics = this.panelTile.CreateGraphics();
            graphics.Clear(panelBackgroundColor);
            if (this.tileBuffer != null)
            {
                Bitmap image = new Bitmap(this.tileBuffer.Width + 1, this.tileBuffer.Height + 1);
                Graphics graphics2 = Graphics.FromImage(image);
                graphics2.DrawImageUnscaled(this.tileBuffer, 1, 1);
                graphics2.Dispose();
                graphics.InterpolationMode = InterpolationMode.NearestNeighbor;
                int x = -scale / 2;
                if (x == 0)
                {
                    x = -1;
                }
                Rectangle destRect = new Rectangle(x, x, image.Width * scale, image.Height * scale);
                graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel);
                image.Dispose();
                graphics.DrawImageUnscaled(this.gridBuffer, 0, 0);
            }
            graphics.Dispose();
        }

        private void PasteCharacter()
        {
            if ((this.selectedIndex >= 0) && (this.selectedIndex < this.font.Tiles.Length))
            {
                object data = Clipboard.GetData("NFTRChar");
                if (data != null)
                {
                    Character character = this.Deserialize(data.ToString()) as Character;
                    if (character != null)
                    {
                        Graphics graphics = Graphics.FromImage(this.font.Tiles[this.selectedIndex]);
                        graphics.Clear(Color.Transparent);
                        graphics.DrawImage(character.Tile, 0, 0);
                        this.font.Properties[this.selectedIndex][0] = (byte) character.OffsetX;
                        this.font.Properties[this.selectedIndex][1] = (byte) character.LetterWidth;
                        this.font.Properties[this.selectedIndex][2] = (byte) character.Width;
                        Character tileByIndex = this.font.GetTileByIndex((ushort) this.selectedIndex);
                        tileByIndex.OffsetX = character.OffsetX;
                        tileByIndex.LetterWidth = character.LetterWidth;
                        tileByIndex.Width = character.Width;
                        this.ChangeSelected(this.selectedIndex);
                    }
                }
            }
        }

        private void PictureBoxMapClick(object sender, EventArgs e)
        {
            if (this.hoverIndex == -1)
            {
                this.ChangeSelected(-1);
            }
            else if (this.selectedIndex == this.hoverIndex)
            {
                this.ChangeSelected(-1);
            }
            else
            {
                this.ChangeSelected(this.hoverIndex);
            }
            this.pictureBoxMap.Select();
        }

        private void PictureBoxMapMouseLeave(object sender, EventArgs e)
        {
            this.ChangeHover(-1, -1, -1);
        }

        private void PictureBoxMapMouseMove(object sender, MouseEventArgs e)
        {
            int x = e.X / (this.font.MaxWidth + 1);
            int y = e.Y / (this.font.MaxHeight + 1);
            int index = (y * this.numCols) + x;
            if ((index < 0) || (index >= this.font.Tiles.Length))
            {
                x = -1;
                y = -1;
                index = -1;
            }
            if (index != this.hoverIndex)
            {
                this.ChangeHover(x, y, index);
            }
        }

        private void PictureBoxMapPreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            int selectedIndex = this.selectedIndex;
            if (e.KeyCode == Keys.Left)
            {
                selectedIndex--;
            }
            else if (e.KeyCode == Keys.Right)
            {
                selectedIndex++;
            }
            else if (e.KeyCode == Keys.Up)
            {
                selectedIndex -= this.numCols;
            }
            else if (e.KeyCode == Keys.Down)
            {
                selectedIndex += this.numCols;
            }
            else if (e.KeyCode == Keys.Prior)
            {
                int num2 = (this.panelMap.Height / (this.font.MaxHeight + 1)) - 1;
                selectedIndex -= num2 * this.numCols;
            }
            else if (e.KeyCode == Keys.Next)
            {
                int num3 = (this.panelMap.Height / (this.font.MaxHeight + 1)) - 1;
                selectedIndex += num3 * this.numCols;
            }
            else if (e.KeyCode == Keys.Home)
            {
                selectedIndex = 0;
            }
            else if (e.KeyCode == Keys.End)
            {
                selectedIndex = this.font.Tiles.Length - 1;
            }
            else if ((e.KeyCode == Keys.C) && ((e.Modifiers & Keys.Control) == Keys.Control))
            {
                this.CopyCharacter();
            }
            else if ((e.KeyCode == Keys.V) && ((e.Modifiers & Keys.Control) == Keys.Control))
            {
                this.PasteCharacter();
            }
            if (selectedIndex < 0)
            {
                selectedIndex = 0;
            }
            if (selectedIndex >= this.font.Tiles.Length)
            {
                selectedIndex = this.font.Tiles.Length - 1;
            }
            if (selectedIndex != this.selectedIndex)
            {
                this.ChangeSelected(selectedIndex);
            }
        }

        private void RecalculateMapDimensions()
        {
            if (this.panelMap.Height != 0)
            {
                int num = (this.panelMap.Width - 20) / (this.font.MaxWidth + 1);
                if (num != this.numCols)
                {
                    this.numCols = num;
                    this.DrawMap();
                }
            }
        }

        private string Serialize(object objectToSerialize)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                new BinaryFormatter().Serialize(stream, objectToSerialize);
                return Convert.ToBase64String(stream.ToArray());
            }
        }

        public void Show(MainForm pParent, NFTRedit.Font pFont, CharacterTable pTable)
        {
            this.parent = pParent;
            this.font = pFont;
            this.table = pTable;
            this.Initialize();
            base.Show();
        }

        public void ShowDialog(MainForm pParent, NFTRedit.Font pFont, CharacterTable pTable)
        {
            this.parent = pParent;
            this.font = pFont;
            this.table = pTable;
            this.Initialize();
            base.ShowDialog();
        }

        private void TextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar != '\b') && !this.validDec.Contains(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void TextBoxTextChanged(object sender, EventArgs e)
        {
            TextBox box = sender as TextBox;
            if (box.Text.Length == 0)
            {
                box.Text = "0";
                box.SelectAll();
            }
            if (!this.isLoading && (this.selectedIndex != -1))
            {
                if (!Utility.AutoApply)
                {
                    this.buttonApply.Enabled = true;
                }
                else
                {
                    if (sender == this.textBoxCharacter)
                    {
                        this.ApplyCharacter();
                    }
                    else if (sender == this.textBoxOffset)
                    {
                        this.ApplyOffset();
                    }
                    else if (sender == this.textBoxWidth)
                    {
                        this.ApplyWidth();
                    }
                    else if (sender == this.textBoxNextOffset)
                    {
                        this.ApplyNextOffset();
                    }
                    this.parent.RedrawDialogBox();
                }
            }
        }

        private void TrackBarIntensityScroll(object sender, EventArgs e)
        {
            if (!this.isLoading)
            {
                this.selectedIntensity = this.trackBarIntensity.Value;
                Bitmap image = new Bitmap(1, 1);
                image.SetPixel(0, 0, panelBackgroundColor);
                Graphics graphics = Graphics.FromImage(image);
                graphics.DrawImageUnscaled(this.font.Dots[this.selectedIntensity], 0, 0);
                graphics.Dispose();
                this.panelPen.BackColor = image.GetPixel(0, 0);
                image.Dispose();
            }
        }
    }
}

